home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 1 Issue 2
/
PDCD-1 - Issue 02.iso
/
_utilities
/
utilities
/
001
/
mnemosyn
/
StubsHack
/
!Help
Wrap
Text File
|
1994-09-02
|
4KB
|
136 lines
StubsHack version 1.00 01 Sep 1994
----------------------------------
Introduction
------------
This is a C library which uses an idea of Martin Ebourne's to enable you
to very easily redirect all calls to the ANSI memory allocation
functions (malloc, realloc, calloc, free) to your own function. This
could be used to add heap-checking to the malloc functions.
The important point is that this happens at *runtime*, so even if you
link your program with a library which was compiled years ago, all
malloc calls within the library will be redirected.
See my 'HeapGraph' library for an example of how to use StubsHack.
Installation
------------
You should move the StubsHack directory into your C path. Don't move any
headers into an existing .h. directory, as they are refered to as
'StubsHack.<headername>.h' in the source.
I find this to be a much simpler way of organising C libraries than
changing C$Path etc. It also protects against problems when header files
from different libraries have the same name.
The actual library is 'C:StubsHack.o.SHLib'.
How StubHack works
------------------
When a program is linked with the object file Stubs, all calls to the
standard C functions like printf, malloc etc. are made (by the linker)
to branch (usually with link) to an address within Stubs.
The instruction at this address isn't the start of the function,
however. It is just a branch to the malloc/etc function inside the
SharedCLib module.
This branch is set to point to the right place within the SharedCLib
module when the program is run, before the main() function is started.
In this way, the SharedCLibrary is used by many different applications
at the same time.
StubsHack simply changes the branch instruction in stubs to be a branch
to a different function. All users of ANSI C functions continue to call
this instruction, so they all end up going to the new function.
What you can do with StubsHack
------------------------------
As well as providing low-level functions to read/change the destination
of any stubs branch instruction, StubsHack provides a general functions
which should be used to intercept/take over the four ANSI C allocation
functions malloc, realloc, calloc and free.
It provides two things:
1) A way of changing the contents of the stubs branch instructions for
malloc/realloc/callos/free, so that it points to one of your
functions.
2) The address (normally within the sharedClibrary) of the original
malloc/realloc/calloc/free function.
The second enables you to make a 'wrapper' function for the ANSI
re/m/calloc/free functions - eg. you redirect all malloc calls to your
own MallocWrap function, and within MallocWrap, you use the actual
shared C lib function pointers to call the original SharedCLib function.
This could be used, for eg., to add heap-verification to your program,
by storing special info in each malloc block, and checking this info is
ok when freeing the block.
For example, the Mnemosyne library does just this to provide
heap-checking and limited protection against overwriting memory etc by
having wrapping functions for malloc/realloc/calloc/free, but only by
#define-ing malloc to mnem_malloc etc, so you have to recompile all
files and libraries used in the program to get the checking.
Obviously, recompiling your source is easy, but recompiling DeskLib (for
eg.) is not a trivial matter.
With StubsHack, it would be easy to make something like Mnemosyne which
would check all memory allocations without needing recompilation of all
libraries etc. In fact, this has been done, and will hopefully be
included with Mnemosyne when it is released.
Other
-----
All the contents of the StubsHack directory are ⌐ Julian Smith, 1994.
Feel free to distribute it, but only distribute the original version. If
you make any changes, please send them to me and I'll send them out with
the next release.
Any suggestions/comments/bug fixes etc are very much welcomed.
- Julian Smith
------------------------
julians@cogsci.ed.ac.uk
------------------------
or:
------------------------
Department of Psychology
University of Edinburgh
7 George Square
Edinburgh
EH8 9JZ
UK
------------------------